function exportXML(obj,docNode)
%Exports TreeWidget data as XML to allow saving current project

% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2013. Jose Maria Garcia-Valdecasas Bernal

%Add all data entries. Used to store TreeWidget info
data_node = docNode.createElement('DATA');
docNode.getDocumentElement.appendChild(data_node);

%loop to read and store grids and control points



node=[]; %#ok<NASGU>
root=obj.m_tree.Root();
for j=0:get(root,'ChildCount')-1
    child=root.getChildAt(j);
    kind=child.getValue();
    kind=char(kind(2));
    if (strcmp(kind,'INPUT'))
        %CGrids found, return child
        node=child;
        IGrids=docNode.createElement('Input_Grids');
        data_node.appendChild(IGrids);
        writeIGridNode(obj,node,IGrids,docNode);
    elseif (strcmp(kind,'COMPUTATIONAL'))
        %CGrids found, return child
        node=child;
        CGrids=docNode.createElement('Comp_Grids');
        data_node.appendChild(CGrids);
        writeCGridNode(obj,node,CGrids,docNode);
    elseif strcmp(kind,'CPOINTS')
        %Control points found, return child
        node=child;
        CPoints=docNode.createElement('Control_Points');
        data_node.appendChild(CPoints);
        writeCPointNode(obj,node,CPoints,docNode);
        
    end
end
end


function writeIGridNode(obj,parent,parentNode,docNode)
if get(parent,'ChildCount')~=0
    for j=0:get(parent,'ChildCount')-1
        %get grid data
        child=parent.getChildAt(j);
        kind=child.getValue();
        kind=char(kind(2));
        name=char(child.getName());
                
        %%nuevo metodo mas corto y sencillo de mantener
        
        grid_entry=docNode.createElement('GRID');
        parentNode.appendChild(grid_entry);
        grid_entry.setAttribute('Type',kind);
        grid_entry.setAttribute('Name',name);
        
        grid=obj.m_grids.(kind);
        
        nRows=size(grid,1);
        
        for k=1:nRows
            nodeName=regexprep(grid{k,1},'\s','_');
            node=docNode.createElement(nodeName);
            node.setAttribute('txt',grid{k,2});
            node.setAttribute('kind',grid{k,3});
            text=data2txt(grid{k,4});
            textNode = docNode.createTextNode(text);
            node.appendChild(textNode);
            grid_entry.appendChild(node);
        end
        
        
        %%delete once it's tested to work with new design
        %{
                %metodo inicial
                
                X0=num2str(obj.getValue('Xmin',kind));
                Y0=num2str(obj.getValue('Ymin',kind));
                DX=num2str(obj.getValue('X Step',kind));
                DY=num2str(obj.getValue('Y Step',kind));
                NX=num2str(obj.getValue('Nx',kind));
                NY=num2str(obj.getValue('Ny',kind));
                if obj.getValue('Swap',kind)
                    Swap='true';
                else
                    Swap='false';
                end
                File=char(obj.getValue('File',kind));
                Filetype=num2str(obj.getValue('filetype',kind));
                Angle=num2str(obj.getValue('Angle',kind));
                Header=num2str(obj.getValue('Header',kind));
                
                generate XML data
                grid_entry=docNode.createElement('GRID');
                parentNode.appendChild(grid_entry);
                
                kind_node=docNode.createElement('TYPE');
                kind_text = docNode.createTextNode(kind);
                kind_node.appendChild(kind_text);
                grid_entry.appendChild(kind_node);
                
                name_node=docNode.createElement('Name');
                name_text = docNode.createTextNode(name);
                name_node.appendChild(name_text);
                grid_entry.appendChild(name_node);
                
                X0_node=docNode.createElement('Xmin');
                X0_text = docNode.createTextNode(X0);
                X0_node.appendChild(X0_text);
                grid_entry.appendChild(X0_node);
                
                Y0_node=docNode.createElement('Ymin');
                Y0_text = docNode.createTextNode(Y0);
                Y0_node.appendChild(Y0_text);
                grid_entry.appendChild(Y0_node);
                
                DX_node=docNode.createElement('X_Step');
                DX_text = docNode.createTextNode(DX);
                DX_node.appendChild(DX_text);
                grid_entry.appendChild(DX_node);
                
                DY_node=docNode.createElement('Y_Step');
                DY_text = docNode.createTextNode(DY);
                DY_node.appendChild(DY_text);
                grid_entry.appendChild(DY_node);
                
                NX_node=docNode.createElement('Nx');
                NX_text = docNode.createTextNode(NX);
                NX_node.appendChild(NX_text);
                grid_entry.appendChild(NX_node);
                
                NY_node=docNode.createElement('Ny');
                NY_text = docNode.createTextNode(NY);
                NY_node.appendChild(NY_text);
                grid_entry.appendChild(NY_node);
                
                Swap_node=docNode.createElement('Swap');
                Swap_text = docNode.createTextNode(Swap);
                Swap_node.appendChild(Swap_text);
                grid_entry.appendChild(Swap_node);
                
                File_node=docNode.createElement('File');
                File_text = docNode.createTextNode(File);
                File_node.appendChild(File_text);
                grid_entry.appendChild(File_node);
                
                Filetype_node=docNode.createElement('Filetype');
                Filetype_text = docNode.createTextNode(Filetype);
                Filetype_node.appendChild(Filetype_text);
                grid_entry.appendChild(Filetype_node);
                
                Angle_node=docNode.createElement('Angle');
                Angle_text = docNode.createTextNode(Angle);
                Angle_node.appendChild(Angle_text);
                grid_entry.appendChild(Angle_node);
                
                Header_node=docNode.createElement('Header');
                Header_text = docNode.createTextNode(Header);
                Header_node.appendChild(Header_text);
                grid_entry.appendChild(Header_node);
                %fin del metodo inicial
        %}
    end
end
end


function writeCGridNode(obj,parent,parentNode,docNode)
nchildren=get(parent,'ChildCount');
for j=0:nchildren-1
    %get info of child
    child=parent.getChildAt(j);
    name=child.getValue();
    name=char(name(2)); %unique name-> cMainXX or cNestXX.
    tag=char(child.getName());
    
    grid_entry=docNode.createElement('GRID');
    parentNode.appendChild(grid_entry);
    grid_entry.setAttribute('Name',tag);
    
    grid=obj.m_grids.(name);
    
    nRows=size(grid,1);
    
    for k=1:nRows
        nodeName=regexprep(grid{k,1},'\s','_');
        node=docNode.createElement(nodeName);
        node.setAttribute('txt',grid{k,2});
        node.setAttribute('kind',grid{k,3});
        text=data2txt(grid{k,4});
        textNode = docNode.createTextNode(text);
        node.appendChild(textNode);
        grid_entry.appendChild(node);
    end
    
    %metodo inicial, borrar cuando se confirme que funciona bien el nuevo
    %{
    X0=num2str(obj.getValue('X0',name));
    Y0=num2str(obj.getValue('Y0',name));
    DX=num2str(obj.getValue('Dx',name));
    DY=num2str(obj.getValue('Dy',name));
    XLength=num2str(obj.getValue('X Length',name));
    YLength=num2str(obj.getValue('Y Length',name));
    Angle=num2str(obj.getValue('Angle',name));
    
    %generate XML data
    grid_entry=docNode.createElement('GRID');
    parentNode.appendChild(grid_entry);
    name_node=docNode.createElement('Name');
    name_text=docNode.createTextNode(tag);
    name_node.appendChild(name_text);
    grid_entry.appendChild(name_node);
    
    X0_node=docNode.createElement('Xmin');
    X0_text=docNode.createTextNode(X0);
    X0_node.appendChild(X0_text);
    grid_entry.appendChild(X0_node);
    
    Y0_node=docNode.createElement('Ymin');
    Y0_text=docNode.createTextNode(Y0);
    Y0_node.appendChild(Y0_text);
    grid_entry.appendChild(Y0_node);
    
    DX_node=docNode.createElement('X_Step');
    DX_text=docNode.createTextNode(DX);
    DX_node.appendChild(DX_text);
    grid_entry.appendChild(DX_node);
    
    DY_node=docNode.createElement('Y_Step');
    DY_text=docNode.createTextNode(DY);
    DY_node.appendChild(DY_text);
    grid_entry.appendChild(DY_node);
    
    XLength_node=docNode.createElement('X_Length');
    XLength_text=docNode.createTextNode(XLength);
    XLength_node.appendChild(XLength_text);
    grid_entry.appendChild(XLength_node);
    
    YLength_node=docNode.createElement('Y_Length');
    YLength_text=docNode.createTextNode(YLength);
    YLength_node.appendChild(YLength_text);
    grid_entry.appendChild(YLength_node);
    
    Angle_node=docNode.createElement('Angle');
    Angle_text = docNode.createTextNode(Angle);
    Angle_node.appendChild(Angle_text);
    grid_entry.appendChild(Angle_node);
    %}
    
    %check if child has also children
    if get(child,'ChildCount')>0
        nested=docNode.createElement('NESTED');
        grid_entry.appendChild(nested);
        writeCGridNode(obj,child,nested,docNode);
    end
    
end

end

function text=data2txt(data)

if islogical(data)
    if data
        text='true';
    else
        text='false';
    end
elseif isnumeric(data)
    text=num2str(data);
else
    text=char(data);
end
end

function writeCPointNode(obj,parent,parentNode,docNode)
if get(parent,'ChildCount')~=0
    for j=0:get(parent,'ChildCount')-1
        %get grid data
        child=parent.getChildAt(j);
        id=child.getValue();
        id=char(id(2));
        name=char(child.getName());
       
        grid_entry=docNode.createElement('POINT');
        parentNode.appendChild(grid_entry);
        
        grid_entry.setAttribute('Name',name);
       
        
        grid=obj.m_grids.(id);
        
        nRows=size(grid,1);
        
        for k=1:nRows
            nodeName=regexprep(grid{k,1},'\s','_');
            node=docNode.createElement(nodeName);
            node.setAttribute('txt',grid{k,2});
            node.setAttribute('kind',grid{k,3});
            text=data2txt(grid{k,4});
            textNode = docNode.createTextNode(text);
            node.appendChild(textNode);
            grid_entry.appendChild(node);
        end      
        
        
    end
end
end
